import {
	type IClient,
	type IClientForm,
	type IClients,
	clientScheme,
	clientsScheme
} from '@/types/client.types'

import { axiosWithAuth } from '@/api/interceptors'

// {
// 	offset,
// 		limit,
// 		columnFilters,
// 		globalFilter,
// 		sorting
// }: {
// 	offset: number
// 	limit: number
// 	columnFilters: any
// 	globalFilter: string
// 	sorting: any
// }

export const clientService = {
	async fetchMany(full_name: string = '') {
		if (full_name) {
			const response = await axiosWithAuth.get<IClients>('/clients/', {
				params: { full_name: full_name }
			})

			return clientsScheme.parse(response.data)
		}
		const response = await axiosWithAuth.get<IClients>('/clients/')

		return clientsScheme.parse(response.data)
	},
	async fetch(id: number) {
		const response = await axiosWithAuth.get<IClient>(`/clients/${id}/`)

		return clientScheme.parse(response.data)
	},
	async update(id: number, data: IClientForm) {
		const response = await axiosWithAuth.put<IClient>(`/clients/${id}/`, data)

		// console.log(response.data)
		return clientScheme.parse(response.data)
	},
	async create(data: IClientForm) {
		const response = await axiosWithAuth.post<IClient>(`/clients/`, data)

		// console.log(response.data)
		return clientScheme.parse(response.data)
	},
	async del(id: number) {
		const response = await axiosWithAuth.delete(`/clients/${id}/`)
		// console.log(response)

		return response.status === 204
	}
}
